<template>
  <div class="container1">
    <div class="header">
      <img src="../assets/logo.png" alt="Your Image">
    </div>
    <div class="news">
      <div class="news-list">
        <h2>Latest News</h2>
        <ul>
          <li v-for="(item, index) in newsList" :key="index" @click="showNews(index)" :class="{ active: index === activeIndex }">{{ item }}</li>
        </ul>
      </div>
      <div class="news-carousel">
        <h2>Breaking News</h2>
        <div class="carousel-item" v-for="(item, index) in breakingNews" :key="index" :class="{ active: activeIndex === index }">
  <h3>{{ item.title }}</h3>
  <p>{{ item.content }}</p>
</div>

      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      newsList: ['News 1', 'News 2', 'News 3', 'News 4'], // 新闻列表
      breakingNews: [ // 轮播的新闻
        {
          title: 'Breaking News 1',
          content: 'Content for Breaking News 1'
        },
        {
          title: 'Breaking News 2',
          content: 'Content for Breaking News 2'
        },
        {
          title: 'Breaking News 3',
          content: 'Content for Breaking News 3'
        },
        {
          title: 'Breaking News 4',
          content: 'Content for Breaking News 4'
        }
      ],
      activeIndex: 0 ,// 当前轮播的新闻的索引
      intervalId: null // 添加一个新的属性用于保存定时器的ID
    }
  },
  methods: {
  showNews(index) {
    this.activeIndex = index; // 点击新闻，选中相应的轮播新闻
    clearInterval(this.intervalId); // 清除之前的轮播定时器
    setTimeout(() => {
      this.intervalId = setInterval(() => {
        this.activeIndex = (this.activeIndex + 1) % this.breakingNews.length; // 切换轮播的新闻
      }, 6000);
    }, 6000);
  }
},
mounted() {
  this.activeIndex = 1; // 默认选中第一条轮播新闻
  this.intervalId = setInterval(() => {
    this.activeIndex = (this.activeIndex + 1) % this.breakingNews.length; // 切换轮播的新闻
  }, 6000);
}
}
</script>

<style>
.container1 {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
}

.header {
  width: 100%;
  height: 25%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.header img {
  max-height: 100%;
  max-width: 100%;
}

.news {
  display: flex;
  flex-direction: row;
  width: 100%;
  height:75%;
  margin-top: 2rem;
}

.news-list {
  width: 33%;
}

.news-list h2 {
  margin-bottom: 1rem;
  font-size: 1.5rem;
  color: #333;
  text-align: center;
}


.news-list ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  margin-top: 1rem;
}

.news-list li {
  margin-bottom: 0.5rem;
  font-size: 1.5rem;
  line-height: 1.8;
  text-align: center;
  cursor: pointer;
}


.news-carousel {
  width: 66%;
  display: flex;
  flex-direction: column;
}

.news-carousel h2 {
  margin-bottom: 1rem;
  text-align: center;
}

.carousel-item {
  display: none;
  padding: 1rem;
}

.carousel-item.active {
  display: block;
}
</style>
